﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Strategy.Core.TradeTypes
{
    public class Candle
    {
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Open time : " + openTime + " Close time : " + closeTime);
            sb.AppendLine("Open = " + open + " ,Close = " + close + " ,High = " + high + " ,Low = " + low + " ,Tick volume = ");
            return sb.ToString(); 
        }
        private DateTime closeTime;
        private DateTime openTime;
        private double open;
        private double close;
        private double high;
        private double low;
        private int tickVolume;
        public DateTime CloseTime { get { return closeTime; } private set { closeTime = value; } }
        public DateTime OpenTime { get { return openTime; } private set { openTime = value; } }
        public double Open { get { return open; } private set { open = value; } }
        public double Close { get { return close; } private set { close = value; } }
        public double High { get { return high; } private set { high = value; } }
        public double Low { get { return low; } private set { low = value; } }
        public int TickVolume { get { return tickVolume; } private set { tickVolume = value; } }
        
        public Candle(DateTime _openTime, TimeSpan _interval) 
        {
            openTime = _openTime;
            closeTime = _openTime.Add(_interval);
            open = 0;
            close = 0;
            high = 0;
            low = 0;
        }
        public void Update(QuoteData qd)
        {
            if (qd.Date < OpenTime || qd.Date > CloseTime)
                 throw new TimeoutException("Candle can't hold QuoteData");
            if (TickVolume == 0)
            {
                Open = qd.Value;
                High = qd.Value;
                Low = qd.Value;
            }
            Close = qd.Value;
            TickVolume++;
            if (qd.Value > High) High = qd.Value;
            if (qd.Value < Low) Low = qd.Value;
        }
    }
}
